﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ApplyUI : MonoBehaviour
{
    public NetworkImage rawImage;
    public Text nameText;
    public Text status;
    public InputField inputField;
    public Text applyBtnText;

    public GameObject priceObj;

    //float rawImageOriginalSize;

    long targetUserId;
    Texture2D m_Texture;
    void Awake()
    {
        //rawImageOriginalSize = rawImage.GetComponent<RectTransform>().sizeDelta.x;
    }

    // Start is called before the first frame update
    void Start()
    {
        
    }

    public void Init(UserInfo userInfo)
    {
        targetUserId = userInfo.userId;

        rawImage.Init(userInfo.icon);
        //var texture = UnityFunctions.GetTextureWithByte(userInfo.icon);

        //if (m_Texture)
        //    DestroyImmediate(m_Texture);
        //m_Texture = texture;

        //if (rawImageOriginalSize <= 0)
        //    rawImageOriginalSize = rawImage.GetComponent<RectTransform>().rect.width;

       // UnityFunctions.SetRawImage(rawImage, texture, rawImageOriginalSize,true);

        nameText.text = userInfo.name;
        status.text = userInfo.status;

        bool free = MGame.instance.MUser.freeApplyCount > 0;

        priceObj.SetActive(!free);
        applyBtnText.text = free ? "免费申请" : "立即申请";
    }

    public void OnClickApply()
    {
        GameDebug.Log("OnClickApply");
        bool free = MGame.instance.MUser.freeApplyCount > 0;
        string message = this.inputField.text;

        if (free || MGame.instance.MUser.diamond >= AppConstantData.applyPrice)
        {
            PanelLoading.Show(5);
            RequestServerController.instance.ReqApply(targetUserId, message, (apply) =>
            {
                PanelLoading.Hide();

                var ui = GetComponentInParent<UserHomePanel>();
                ui.OnClickCloseAppUI();

                ApplyUserPanel view = ui.UIController.ShowUI<ApplyUserPanel>();
                view.Init(apply);
            });
        }
        else
        {
            var ui = GetComponentInParent<UserHomePanel>();
            var dne = ui.UIController.ShowUI<DiamondNotEnoughPanel>();
            dne.Init(AppConstantData.applyPrice, MGame.instance.MUser.diamond);
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    void OnDestroy()
    {

    }
}
